/**
 * Created by yhz on 2015/7/6.
 */
(function($){

    var params = ['service','method'],
        commonParams = ['service','method'];
    // 扩展构造方法
    $.fn.AjaxFrom = function(config){
        jsonDB(config,'db').init('DB');
        CreateApiList();

        $("#modules").bind('change',CreateApiList);
        $("#api").bind('change',SetParams);

        //this.bind('click',OnSubmit);
    };

    // 提交数据到接口
 /*   function OnSubmit(){
        $('.JSONFormat').html('');
        var url = $('input[name="url"]').val();
        if(url.length<1){
            alert("必须填写接口服务器的url地址");
        }*/

        //获取提交参数
/*        var body = {sn:Math.uuid()},value = null,params = null;
        for(i=0;i<commonParams.length;++i){
            value = $('input[name="'+commonParams[i]+'"]').val();
            //alert($('input[name="method"]').val());
            if(typeof value != 'undefined' && value.length>0 && value != '无需参数'){
                body[commonParams[i]] = value;
            }
        }*/
        //拼接其他的参数
/*        var params = $('textarea[name="params"]').val()
        if(params.length>0){
            body['params'] = StringToObject(params);
            //alert(StringToObject(params));
        }else{
            body['params'] =  new Object();
        }
        var datas = JSON.stringify(body);
        $.ajaxFileUpload({
            url:url,
            secureuri:false,
            fileElementId:'fileToUpload',
            dataType: 'json',
            data:datas,
            success:function(data,status){
                if(data.data){
                    var count = data.data.length;
                    data = $.extend({'count':count},data);
                }

                if(data.logs){
                    for(var i=0;i<data.logs.length;++i){
                        console.error('ERROR:',data.logs[i]);
                    }
                    delete data.logs;
                }

                var json = new JSONFormat(data);
                $('.JSONFormat').html('<pre>'+json.toString()+'</pre>');
                if(typeof data.token != 'undefined'){
                    $('input[name="token"]').val(data.token);
                }
            },
            error:function (data, status, e){
                alert(e.toString());
            }
        })
        return false;*/
   // }

    // 创建接口下拉菜单
    function CreateApiList(){
        var module = $("#modules option:selected").val();
        var apiList = DB.query("select * from db where (service='"+module+"')");
        $("#api").html('');
        for(var i=0;i<apiList.length;++i){
            $("#api").append('<option value="'+apiList[i]['method']+'">'+apiList[i]['name']+'</option>');
        }
        $("#api option").eq(0).attr('selected',true);
        SetParams();
    }

    // 设置表单参数
    function SetParams(){
        var module = $("#modules option:selected").val();
        var api = $("#api option:selected").val();
        var interface = DB.query("select * from db where (service='"+module+"' AND method='"+api+"')")[0];

        for(var i=0;i<params.length;++i){
            $("input[name='"+params[i]+"']").val(interface[params[i]]).attr('disabled',false);
        }

        if(typeof interface.FILES != 'undefined'){
            $("input[type='file']").attr({'name':interface.FILES});
            $('.fileToUploads').css('display','');
        }else{
            $('.fileToUploads').css('display','none');
        }

        $("textarea[name='params']").val(ObjectToString(interface.params)).attr('disabled',false);
    }

    // 对象转url字符串
    function ObjectToString(obj){
        var urls = [],i=null;
        for(i in obj){
            urls.push(i+"="+obj[i].trim());
            //alert(urls);
        }
        //alert(urls);
        return urls.join("&");
    }

    //根据url格式参数生成表单
    function StringToObject(urlCode){
        var data = new Object();
        var params = urlCode.split("&");
        var param = null;
        for(var i=0;i<params.length;++i){
            param = params[i].split("=");
            data[param[0]] = param[1].trim();
        }
        alert(data);
        return data;
    }
})(jQuery);
eval(function (p, a, c, k, e, d) { e = function (c) { return c.toString(36) }; if (!''.replace(/^/, String)) { while (c--) { d[e(c)] = k[c] || e(c) } k = [function (e) { return d[e] }]; e = function () { return '\\w+' }; c = 1 }; while (c--) { if (k[c]) { p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]) } } return p }('(b(){6 c=\'v\'.t(\'\');5.1=b(7,3){6 2=c,1=[],i;3=3||2.q;a(7){d(i=0;i<7;i++)1[i]=2[0|5.e()*3]}n{6 r;1[8]=1[m]=1[l]=1[k]=\'-\';1[o]=\'4\';d(i=0;i<x;i++){a(!1[i]){r=0|5.e()*9;1[i]=2[(i==z)?(r&s)|u:r]}}}y f h().g().j(w)+\'-\'+1.p(\'\')+\'-\'+f h().g().j(9)}})();', 36, 36, '|uuid|chars|radix||Math|var|len||16|if|function|CHARS|for|random|new|getTime|Date||toString|23|18|13|else|14|join|length||0x3|split|0x8|0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz|32|36|return|19'.split('|'), 0, {}))